class Solution {
public:
    int firstMissingPositive(vector<int>& nums)
    {
        int size = nums.size(), ans = 0, i = 0;
        while(i < size)
            if(nums[i] == i + 1) ++i;
            else if(nums[i] > size || nums[i] <= 0) ++i;
            else if(nums[i] == nums[nums[i] - 1]) ++i;
            else swap(nums[i], nums[nums[i] - 1]);
        while(ans < size && nums[ans] == ans + 1) ++ans;
        return ans + 1;
    }
};